<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/17
 * Time: 上午10:33
 */

namespace app\index\controller;


use app\common\controller\Adverput;
use app\common\model\Ggk;
use app\common\model\GgkLog;
use app\common\model\Paylog;
use app\common\model\PaymentAd;
use app\common\model\PaymentAdViewLog;
use app\common\model\WxUser;
use think\Controller;
use app\common\model\Ad;
use app\common\model\AdLog;
use think\Request;

class Adview extends Controller
{

    //获取广告 增加浏览记录(AJAX
    public function getad($orderid)
    {
        $Paylog=new Paylog();
        $payinfo = $Paylog->where(['orderid'=>$orderid])->field('mid,openid')->find();
        $Adverput = new Adverput();
        $adinfo = $Adverput->getmachadver($payinfo['mid']);
        if ($adinfo['code'] == 0) {
            $PaymentAdViewLog = new PaymentAdViewLog();
            $isorder = $PaymentAdViewLog->where(['orderid'=>$orderid])->find();
            if (!$isorder) {
                $data['orderid'] = $orderid;
                $data['openid'] = $payinfo['openid'];
                $data['view'] = 1;
                $data['ad_id'] = $adinfo['data']['id'];
                $PaymentAdViewLog->save($data);
                $adinfo['data']['view_id'] = $PaymentAdViewLog->id;
            }else{
                $adinfo['data']['view_id'] = $isorder['id'];
            }
        }
        return json($adinfo);
    }

    //增加点击次数(AJAX
    public function adviewlog()
    {
        if (Request()->isAjax()){
            $PaymentAdViewLog = new PaymentAdViewLog();
            $param = request()->post();
            $PaymentAd = new PaymentAd();
            $res = $PaymentAdViewLog->where(['id'=>$param['view_id']])->setInc('click');
            $ad_id = $PaymentAdViewLog->where(['id'=>$param['view_id']])->value('ad_id');
            $PaymentAd->where(['id'=>$ad_id])->setInc('click');
            if ($res){
                $data['code'] = 0;
                $data['message'] = 'success';
                $data['ad_url'] = $PaymentAd->where(['id'=>$ad_id])->value('ad_url');
            }else{
                $data['code'] = 1;
                $data['message'] = 'error';
            }
            return json($data);
        }
    }

    //广告跳转(增加记录 增加次数
    public function getadvert($orderid)
    {
        $Paylog=new Paylog();
        $payinfo = $Paylog->where(['orderid'=>$orderid])->field('mid,openid,pay_fs')->find();
        if ($payinfo['pay_fs'] == 1){
            $this->redirect('https://qr.alipay.com/c1x04397pkgzg892ott3m0f');
        }
        $Adverput = new Adverput();
        $adinfo = $Adverput->getmachadver($payinfo['mid']);
        if ($adinfo['code'] == 0) {

            $PaymentAdViewLog = new PaymentAdViewLog();
            $isorder = $PaymentAdViewLog->where(['orderid'=>$orderid])->find();
            if (!$isorder) {
                $data['orderid'] = $orderid;
                $data['openid'] = $payinfo['openid'];
                $data['view'] = 1;
                $data['ad_id'] = $adinfo['data']['id'];
                $data['click'] = 1;
                $PaymentAdViewLog->save($data);
                $adinfo['data']['view_id'] = $PaymentAdViewLog->id;
            }else{
                $adinfo['data']['view_id'] = $isorder['id'];
            }
//            file_put_contents('/data2/html/payos/payad.txt', json_encode($adinfo), 8);
            $this->redirect($adinfo['data']['ad_url']);
        }
    }


    //广告类别
    //0 / 页面展示    1 / 直接跳转
    public function getadtype($orderid){
        $Paylog=new Paylog();
        $payinfo = $Paylog->where(['orderid'=>$orderid])->field('mid,openid')->find();
        $Adverput = new Adverput();
        $adinfo = $Adverput->getmachadver($payinfo['mid']);
        if ($adinfo['code'] == 0) {
            $data['ad_type'] = $adinfo['data']['type'];
        }else{
            $data['ad_type'] = 'on';
        }
        return $data;
    }


}